/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cbaServer;

import commonSources.Cliente;
import commonSources.Conta;
import commonSources.Funcionario;
import commonSources.Painel;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Hashtable;

/**
 *
 * @author Rente
 */
public class RemoteOperations extends UnicastRemoteObject implements BankServerRemoteInterface {

    private BankServer bs;

    public RemoteOperations(BankServer bs) throws RemoteException {
        this.bs = bs;
    }

    public synchronized void adicionaCliente(Cliente c) {
        bs.getListaClientes().put(c.getId(), c);
        bs.getHandler().saveClientes(bs.getListaClientes());
    }

    public synchronized void removeCliente(String id) {
        bs.getListaClientes().remove(id);
        bs.getHandler().saveClientes(bs.getListaClientes());
    }

    public Hashtable<String, Painel> getListaPaneis() {
        return bs.getListaPaneis();
    }

    public synchronized void adicionaPainel(Painel p) {
        bs.getListaPaneis().put(p.getId(), p);
        bs.getHandler().savePanels(bs.getListaPaneis());
    }

    public synchronized void removePainel(String id) {
        bs.getListaPaneis().remove(id);
        bs.getHandler().savePanels(bs.getListaPaneis());
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.7EA777C6-F9EC-52C5-B7A2-FF6C9E616E5D]
    // </editor-fold>
    public Painel getPainel(String id) {
        return bs.getListaPaneis().get(id);
    }

    public synchronized void associaPainelCliente(String idCliente, String idPainel) {
        Cliente c = getCliente(idCliente);
        Painel p = getPainel(idPainel);
        c.setPainel(p);
        bs.getHandler().saveClientes(bs.getListaClientes());
    }

    public Cliente getCliente(String idCliente) {
        return bs.getListaClientes().get(idCliente);
    }

    public synchronized void eliminaConta(String numConta, String idCliente) {
        getCliente(idCliente).getContas().remove(numConta);
        guardaClientes();
    }

    public synchronized void novaContaCliente(String idCliente, Conta c) {
        getCliente(idCliente).getContas().put(c.getNumConta(), c);
        guardaClientes();
    }

    public Hashtable<String, Cliente> getListaClientes() {
        return bs.getListaClientes();
    }

    public Hashtable<String, Funcionario> getListaFuncs() {
        return bs.getListaFuncionarios();
    }

    public synchronized Conta getConta(String numeroConta) {
        System.out.println("busco conta: " + numeroConta);
        for (Cliente cliente : bs.getListaClientes().values()) {
            if (cliente.getContas().containsKey(numeroConta)) {
                return cliente.getConta(numeroConta);
            }
        }
        System.out.println("Nao achei... :'(");
        return null;
    }

    public synchronized void guardaClientes() {
        bs.getHandler().saveClientes(bs.getListaClientes());
    }

    public synchronized void removeFuncionario(String id) {
        bs.getListaFuncionarios().remove(id);
        bs.getHandler().saveFuncionarios(bs.getListaFuncionarios());
    }

    public synchronized void adicionaFuncionario(Funcionario f) {
        bs.getListaFuncionarios().put(f.getIdFuncionario(), f);
        bs.getHandler().saveFuncionarios(bs.getListaFuncionarios());
    }
}
